{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple\n",
      "Looking in links: https://download.pytorch.org/whl/torch_stable.html\n",
      "Collecting torch==1.4.0+cpu\n",
      "  Downloading https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp36-cp36m-linux_x86_64.whl (127.2 MB)\n",
      "\u001b[K     |████████████████████████████████| 127.2 MB 66 kB/s  eta 0:00:01MB 417 kB/s eta 0:04:52  | 22.5 MB 726 kB/s eta 0:02:25 MB 146 kB/s eta 0:10:08kB/s eta 0:04:15�██████████▌                | 61.5 MB 166 kB/s eta 0:06:342 kB/s eta 0:01:13███████████▏              | 68.3 MB 366 kB/s eta 0:02:41��██████████████▋             | 73.8 MB 321 kB/s eta 0:02:47�████████▏          | 84.0 MB 1.2 MB/s eta 0:00:36 eta 0:02:38�███████████▎       | 96.6 MB 277 kB/s eta 0:01:51�███████████▉       | 98.8 MB 277 kB/s eta 0:01:43 | 104.3 MB 7.0 MB/s eta 0:00:04 | 105.7 MB 7.0 MB/s eta 0:00:04.2 MB 394 kB/s eta 0:00:38\n",
      "\u001b[?25hCollecting torchvision==0.5.0+cpu\n",
      "  Downloading https://download.pytorch.org/whl/cpu/torchvision-0.5.0%2Bcpu-cp36-cp36m-linux_x86_64.whl (5.4 MB)\n",
      "\u001b[K     |████████████████████████████████| 5.4 MB 524 kB/s eta 0:00:01███████████▎         | 3.7 MB 315 kB/s eta 0:00:06\n",
      "\u001b[?25hRequirement already satisfied: pillow>=4.1.1 in /opt/conda/lib/python3.6/site-packages (from torchvision==0.5.0+cpu) (8.0.1)\n",
      "Requirement already satisfied: six in /opt/conda/lib/python3.6/site-packages (from torchvision==0.5.0+cpu) (1.15.0)\n",
      "Requirement already satisfied: numpy in /opt/conda/lib/python3.6/site-packages (from torchvision==0.5.0+cpu) (1.19.4)\n",
      "Installing collected packages: torch, torchvision\n",
      "\u001b[33m  WARNING: The scripts convert-caffe2-to-onnx and convert-onnx-to-caffe2 are installed in '/home/admin/.local/bin' which is not on PATH.\n",
      "  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\n",
      "Successfully installed torch-1.4.0+cpu torchvision-0.5.0+cpu\n",
      "\u001b[33mWARNING: You are using pip version 21.0.1; however, version 21.3.1 is available.\n",
      "You should consider upgrading via the '/opt/conda/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd import Variable\n",
    "import torch.nn as nn\n",
    "import torchvision\n",
    "import torch.utils.data as Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "BATCH_SIZE = 64\n",
    "#学习率，学习率一般为0.01，0.1等等较小的数，为了在梯度下降求解时避免错过最优解\n",
    "LR = 0.001\n",
    "EPOCH = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple\n",
      "Collecting ipywidgets\n",
      "  Downloading https://mirrors.aliyun.com/pypi/packages/6b/bb/285066ddd710779cb69f03d42fa72fbfe4352b4895eb6abab551eae1535a/ipywidgets-7.6.5-py2.py3-none-any.whl (121 kB)\n",
      "\u001b[K     |████████████████████████████████| 121 kB 7.5 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting widgetsnbextension~=3.5.0\n",
      "  Downloading https://mirrors.aliyun.com/pypi/packages/d7/31/7c1107fa30c621cd1d36410e9bbab86f6a518dc208aaec01f02ac6d5c2d2/widgetsnbextension-3.5.2-py2.py3-none-any.whl (1.6 MB)\n",
      "\u001b[K     |████████████████████████████████| 1.6 MB 119.2 MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: traitlets>=4.3.1 in /opt/conda/lib/python3.6/site-packages (from ipywidgets) (4.3.3)\n",
      "Collecting jupyterlab-widgets>=1.0.0\n",
      "  Downloading https://mirrors.aliyun.com/pypi/packages/18/4d/22a93473bca99c80f2d23f867ebbfee2f6c8e186bf678864eec641500910/jupyterlab_widgets-1.0.2-py3-none-any.whl (243 kB)\n",
      "\u001b[K     |████████████████████████████████| 243 kB 104.9 MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: ipykernel>=4.5.1 in /opt/conda/lib/python3.6/site-packages (from ipywidgets) (5.4.2)\n",
      "Requirement already satisfied: ipython-genutils~=0.2.0 in /opt/conda/lib/python3.6/site-packages (from ipywidgets) (0.2.0)\n",
      "Requirement already satisfied: ipython>=4.0.0 in /opt/conda/lib/python3.6/site-packages (from ipywidgets) (7.9.0)\n",
      "Requirement already satisfied: nbformat>=4.2.0 in /opt/conda/lib/python3.6/site-packages (from ipywidgets) (5.0.8)\n",
      "Requirement already satisfied: jupyter-client in /opt/conda/lib/python3.6/site-packages (from ipykernel>=4.5.1->ipywidgets) (6.1.7)\n",
      "Requirement already satisfied: tornado>=4.2 in /opt/conda/lib/python3.6/site-packages (from ipykernel>=4.5.1->ipywidgets) (6.1)\n",
      "Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (2.0.10)\n",
      "Requirement already satisfied: jedi>=0.10 in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (0.17.2)\n",
      "Requirement already satisfied: pickleshare in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (0.7.5)\n",
      "Requirement already satisfied: backcall in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (0.2.0)\n",
      "Requirement already satisfied: setuptools>=18.5 in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (51.1.1)\n",
      "Requirement already satisfied: decorator in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (4.4.2)\n",
      "Requirement already satisfied: pygments in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (2.7.3)\n",
      "Requirement already satisfied: pexpect in /opt/conda/lib/python3.6/site-packages (from ipython>=4.0.0->ipywidgets) (4.8.0)\n",
      "Requirement already satisfied: parso<0.8.0,>=0.7.0 in /opt/conda/lib/python3.6/site-packages (from jedi>=0.10->ipython>=4.0.0->ipywidgets) (0.7.1)\n",
      "Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /opt/conda/lib/python3.6/site-packages (from nbformat>=4.2.0->ipywidgets) (3.2.0)\n",
      "Requirement already satisfied: jupyter-core in /opt/conda/lib/python3.6/site-packages (from nbformat>=4.2.0->ipywidgets) (4.7.0)\n",
      "Requirement already satisfied: pyrsistent>=0.14.0 in /opt/conda/lib/python3.6/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (0.17.3)\n",
      "Requirement already satisfied: importlib-metadata in /opt/conda/lib/python3.6/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (3.3.0)\n",
      "Requirement already satisfied: attrs>=17.4.0 in /opt/conda/lib/python3.6/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (20.3.0)\n",
      "Requirement already satisfied: six>=1.11.0 in /opt/conda/lib/python3.6/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (1.15.0)\n",
      "Requirement already satisfied: wcwidth in /opt/conda/lib/python3.6/site-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets) (0.2.5)\n",
      "Requirement already satisfied: notebook>=4.4.1 in /opt/conda/lib/python3.6/site-packages (from widgetsnbextension~=3.5.0->ipywidgets) (6.1.6)\n",
      "Requirement already satisfied: jinja2 in /opt/conda/lib/python3.6/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.11.2)\n",
      "Requirement already satisfied: pyzmq>=17 in /opt/conda/lib/python3.6/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (20.0.0)\n",
      "Requirement already satisfied: nbconvert in /opt/conda/lib/python3.6/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (6.0.7)\n",
      "Requirement already satisfied: argon2-cffi in /opt/conda/lib/python3.6/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (20.1.0)\n",
      "Requirement already satisfied: terminado>=0.8.3 in /opt/conda/lib/python3.6/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.9.1)\n",
      "Requirement already satisfied: prometheus-client in /opt/conda/lib/python3.6/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.9.0)\n",
      "Requirement already satisfied: Send2Trash in /opt/conda/lib/python3.6/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.5.0)\n",
      "Requirement already satisfied: python-dateutil>=2.1 in /opt/conda/lib/python3.6/site-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets) (2.8.1)\n",
      "Requirement already satisfied: ptyprocess in /opt/conda/lib/python3.6/site-packages (from terminado>=0.8.3->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.7.0)\n",
      "Requirement already satisfied: cffi>=1.0.0 in /opt/conda/lib/python3.6/site-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.14.4)\n",
      "Requirement already satisfied: pycparser in /opt/conda/lib/python3.6/site-packages (from cffi>=1.0.0->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.20)\n",
      "Requirement already satisfied: typing-extensions>=3.6.4 in /opt/conda/lib/python3.6/site-packages (from importlib-metadata->jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (3.7.4.3)\n",
      "Requirement already satisfied: zipp>=0.5 in /opt/conda/lib/python3.6/site-packages (from importlib-metadata->jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (3.4.0)\n",
      "Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/lib/python3.6/site-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.1.1)\n",
      "Requirement already satisfied: pandocfilters>=1.4.1 in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.4.3)\n",
      "Requirement already satisfied: bleach in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (3.2.1)\n",
      "Requirement already satisfied: defusedxml in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.6.0)\n",
      "Requirement already satisfied: entrypoints>=0.2.2 in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.3)\n",
      "Requirement already satisfied: testpath in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.4.4)\n",
      "Requirement already satisfied: mistune<2,>=0.8.1 in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.8.4)\n",
      "Requirement already satisfied: jupyterlab-pygments in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.1.2)\n",
      "Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in /opt/conda/lib/python3.6/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.1)\n",
      "Requirement already satisfied: async-generator in /opt/conda/lib/python3.6/site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.10)\n",
      "Requirement already satisfied: nest-asyncio in /opt/conda/lib/python3.6/site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.4.3)\n",
      "Requirement already satisfied: webencodings in /opt/conda/lib/python3.6/site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.1)\n",
      "Requirement already satisfied: packaging in /opt/conda/lib/python3.6/site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (20.8)\n",
      "Requirement already satisfied: pyparsing>=2.0.2 in /opt/conda/lib/python3.6/site-packages (from packaging->bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.4.7)\n",
      "Installing collected packages: widgetsnbextension, jupyterlab-widgets, ipywidgets\n",
      "Successfully installed ipywidgets-7.6.5 jupyterlab-widgets-1.0.2 widgetsnbextension-3.5.2\n",
      "\u001b[33mWARNING: You are using pip version 21.0.1; however, version 21.3.1 is available.\n",
      "You should consider upgrading via the '/opt/conda/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip3 install ipywidgets --user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to .\\data/MNIST/raw/train-images-idx3-ubyte.gz\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0d7c1d40351045e390777f4fd56b9823",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Extracting .\\data/MNIST/raw/train-images-idx3-ubyte.gz to .\\data/MNIST/raw\n",
      "Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to .\\data/MNIST/raw/train-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "60aa24f45ce44c2a8ce3baa3110cbf41",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Extracting .\\data/MNIST/raw/train-labels-idx1-ubyte.gz to .\\data/MNIST/raw\n",
      "Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to .\\data/MNIST/raw/t10k-images-idx3-ubyte.gz\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2c7d350174e8488e8964dd33029fb591",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Extracting .\\data/MNIST/raw/t10k-images-idx3-ubyte.gz to .\\data/MNIST/raw\n",
      "Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to .\\data/MNIST/raw/t10k-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "586b4856972044e28f31f24565f4d4ec",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Extracting .\\data/MNIST/raw/t10k-labels-idx1-ubyte.gz to .\\data/MNIST/raw\n",
      "Processing...\n",
      "Done!\n"
     ]
    }
   ],
   "source": [
    "train_data = torchvision.datasets.MNIST(root='.\\data', \n",
    "                                        train=True, \n",
    "                                        transform=torchvision.transforms.ToTensor(),  \n",
    "                                        download=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=2 )\n",
    "#每个batch_size的shape为[64, 1, 28, 28]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#测试集操作和上面注释一样\n",
    "test_data = torchvision.datasets.MNIST(\n",
    "    root='.\\data',\n",
    "    train = False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/admin/.local/lib/python3.6/site-packages/torchvision/datasets/mnist.py:60: UserWarning: test_data has been renamed data\n",
      "  warnings.warn(\"test_data has been renamed data\")\n",
      "/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n",
      "/home/admin/.local/lib/python3.6/site-packages/torchvision/datasets/mnist.py:50: UserWarning: test_labels has been renamed targets\n",
      "  warnings.warn(\"test_labels has been renamed targets\")\n"
     ]
    }
   ],
   "source": [
    "test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1), volatile=True).type(torch.FloatTensor)[:2000]/255.0\n",
    "#标签取前2000\n",
    "test_y = test_data.test_labels[:2000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CNN(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(CNN, self).__init__()\n",
    "        #前面都是规定结构\n",
    "        #第一个卷积层，这里使用快速搭建发搭建网络\n",
    "        self.conv1 = nn.Sequential(\n",
    "            nn.Conv2d(\n",
    "                in_channels=1,#灰度图，channel为一\n",
    "                out_channels=16,#输出channels自己设定\n",
    "                kernel_size=3,#卷积核大小\n",
    "                stride=1,#步长\n",
    "                padding=1#padding=（kernel_size-stride）/2   往下取整\n",
    "            ),\n",
    "            nn.ReLU(),#激活函数，线性转意识到非线性空间\n",
    "            nn.MaxPool2d(kernel_size=2)#池化操作，降维，取其2x2窗口最大值代表此窗口，因此宽、高减半，channel不变\n",
    "        )\n",
    "        #此时shape为[16, 14, 14]\n",
    "        self.conv2 = nn.Sequential(\n",
    "            nn.Conv2d(\n",
    "                in_channels=16,\n",
    "                out_channels=32,\n",
    "                kernel_size=3,\n",
    "                stride=1,\n",
    "                padding=1\n",
    "            ),\n",
    "            nn.ReLU(),\n",
    "            nn.MaxPool2d(kernel_size=2)\n",
    "        )\n",
    "        #此时shape为[32, 7, 7]\n",
    "        #定义全连接层，十分类，并且全连接接受两个参数，因此为[32*7*7, 10]\n",
    "        self.prediction = nn.Linear(32*7*7, 10)\n",
    "        #前向传播过程\n",
    "    def forward(self, x):\n",
    "        x = self.conv1(x)\n",
    "        x = self.conv2(x)\n",
    "        x = x.view(x.size(0), -1)\n",
    "        output = self.prediction(x)\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "cnn = CNN()\n",
    "\n",
    "#大数据常用Adam优化器，参数需要model的参数，以及学习率\n",
    "optimizer = torch.optim.Adam(cnn.parameters(), LR)\n",
    "#定义损失函数，交叉熵\n",
    "loss_func = nn.CrossEntropyLoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 0 |test accuracy：0.1025\n",
      "epoch: 0 |test accuracy：0.7275\n",
      "epoch: 0 |test accuracy：0.8305\n",
      "epoch: 0 |test accuracy：0.8900\n",
      "epoch: 0 |test accuracy：0.9130\n",
      "epoch: 0 |test accuracy：0.9205\n",
      "epoch: 0 |test accuracy：0.9325\n",
      "epoch: 0 |test accuracy：0.9415\n",
      "epoch: 0 |test accuracy：0.9510\n",
      "epoch: 0 |test accuracy：0.9565\n",
      "epoch: 0 |test accuracy：0.9620\n",
      "epoch: 0 |test accuracy：0.9665\n",
      "epoch: 0 |test accuracy：0.9640\n",
      "epoch: 0 |test accuracy：0.9640\n",
      "epoch: 0 |test accuracy：0.9690\n",
      "epoch: 0 |test accuracy：0.9695\n",
      "epoch: 0 |test accuracy：0.9655\n",
      "epoch: 0 |test accuracy：0.9705\n",
      "epoch: 0 |test accuracy：0.9710\n"
     ]
    }
   ],
   "source": [
    "#训练阶段\n",
    "for epoch in range(EPOCH):\n",
    "    #step,代表现在第几个batch_size\n",
    "    #batch_x 训练集的图像\n",
    "    #batch_y 训练集的标签\n",
    "    for step, (batch_x, batch_y) in enumerate(train_loader):\n",
    "        #model只接受Variable的数据，因此需要转化\n",
    "        b_x = Variable(batch_x)\n",
    "        b_y = Variable(batch_y)\n",
    "        #将b_x输入到model得到返回值\n",
    "        output = cnn(b_x)\n",
    "        #计算误差\n",
    "        loss = loss_func(output, b_y)\n",
    "        #将梯度变为0\n",
    "        optimizer.zero_grad()\n",
    "        #反向传播\n",
    "        loss.backward()\n",
    "        #优化参数\n",
    "        optimizer.step()\n",
    "        #打印操作，用测试集检验是否预测准确\n",
    "        if step%50 == 0:\n",
    "            test_output = cnn(test_x)\n",
    "            #squeeze将维度值为1的除去，例如[64, 1, 28, 28]，变为[64, 28, 28]\n",
    "            pre_y = torch.max(test_output, 1)[1].data.squeeze()\n",
    "            #总预测对的数除总数就是对的概率\n",
    "            accuracy = float((pre_y == test_y).sum()) / float(test_y.size(0))\n",
    "            print(\"epoch:\", epoch,  \"|test accuracy：%.4f\" %accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-1.4503e+00, -4.6007e+00, -9.6238e-01, -5.9879e+00, -1.0584e+00,\n",
      "          5.6291e-01,  8.1469e+00, -1.0287e+01, -2.1331e+00, -1.1494e+01],\n",
      "        [-8.1156e+00, -3.6094e+00, -8.1621e+00, -3.3691e+00,  2.8038e+00,\n",
      "         -3.9118e+00, -1.0886e+01,  4.3364e-01, -6.7522e-01,  6.1097e+00],\n",
      "        [-7.4065e+00, -7.1274e+00, -3.1527e+00,  5.8281e+00, -9.0858e+00,\n",
      "         -1.6791e-01, -9.1932e+00, -5.2820e+00,  1.8740e-01,  2.0745e+00],\n",
      "        [-1.0323e+01, -7.4515e+00, -5.2741e+00, -5.9760e+00, -1.5739e+00,\n",
      "         -8.9230e-01, -5.0296e+00, -6.0679e+00,  5.1783e+00, -3.0231e+00],\n",
      "        [-5.5582e+00, -4.5766e+00, -2.3470e+00, -2.0348e+00, -4.4309e+00,\n",
      "         -6.0706e+00, -1.4012e+01,  8.4991e+00, -4.2366e+00,  3.3927e+00],\n",
      "        [ 3.3718e+00, -1.5164e+01, -1.0147e+00, -5.0072e+00, -5.7901e+00,\n",
      "         -3.4816e+00, -3.0037e+00, -2.6456e+00, -5.9930e+00, -3.0275e+00],\n",
      "        [-3.7193e+00, -5.0538e+00, -3.6388e+00, -3.6556e+00, -6.7342e-01,\n",
      "         -3.0981e+00,  6.7050e+00, -5.2550e+00, -2.8001e+00, -8.5979e+00],\n",
      "        [-7.4287e+00, -1.0257e+01, -3.5650e+00, -6.5591e-01, -1.6389e+01,\n",
      "          9.4847e+00, -5.1903e+00, -1.2335e+01,  1.2111e+00, -3.0674e+00],\n",
      "        [-1.0876e+01, -9.0846e+00, -1.3711e+00,  3.0230e+00, -1.4091e+01,\n",
      "          4.4967e+00, -9.0854e+00, -6.0472e+00, -6.9979e-01, -8.2187e+00],\n",
      "        [-3.7660e+00,  6.1019e+00, -3.8148e+00, -1.8237e+00, -7.3425e-01,\n",
      "         -3.2520e+00, -3.3696e+00,  1.1708e+00, -1.7000e+00, -1.2404e+00],\n",
      "        [ 7.3624e+00, -1.6915e+01, -1.2618e+00, -9.0615e+00, -8.4407e+00,\n",
      "         -5.0035e+00, -4.8636e-01, -6.3463e+00, -3.6429e+00, -3.6789e+00],\n",
      "        [-3.8103e+00, -1.1496e+01, -3.7822e+00, -1.4261e+00, -9.6063e+00,\n",
      "          1.3124e-01, -4.7264e+00, -9.4509e+00,  5.2145e+00, -5.0941e-01],\n",
      "        [-3.3109e+00,  6.1234e+00, -1.3746e+00, -3.8609e+00, -1.5751e-01,\n",
      "         -6.7171e+00, -3.5732e+00, -3.1427e+00, -1.2849e-01, -6.0578e+00],\n",
      "        [-8.3232e+00, -1.3403e+01, -7.6155e+00, -1.2007e+00, -9.4231e+00,\n",
      "          9.7554e+00, -3.8582e+00, -1.3881e+01,  6.2024e-01, -2.9908e+00],\n",
      "        [ 9.2006e+00, -1.2444e+01, -4.6645e+00, -6.7743e+00, -1.9334e+01,\n",
      "          1.4930e+00, -7.7706e+00, -9.8866e+00,  1.5322e+00, -2.9096e+00],\n",
      "        [-9.3759e+00, -3.5946e+00, -6.7600e+00, -4.1979e+00,  7.1846e+00,\n",
      "         -2.8906e+00, -7.0868e+00, -6.7147e-01, -2.4906e-02,  1.9779e+00],\n",
      "        [-3.3582e+00, -1.1116e+01, -2.1543e+00, -9.2635e+00, -4.9065e+00,\n",
      "         -4.8525e+00,  9.6806e+00, -9.6001e+00, -1.7322e+00, -8.3743e+00],\n",
      "        [ 8.2671e+00, -1.0544e+01, -5.0413e-01, -7.7474e+00, -5.9910e+00,\n",
      "         -6.3762e+00,  9.0284e-01, -5.9609e+00, -1.0485e+00, -3.9544e+00],\n",
      "        [-5.2112e+00, -3.3640e+00,  4.6105e+00, -1.0086e+00, -3.6859e+00,\n",
      "         -6.4910e+00, -4.1889e+00, -4.6033e+00,  8.6362e-01, -4.9415e+00],\n",
      "        [ 1.1417e+00, -6.1824e+00, -6.8579e+00, -5.5890e+00, -1.1460e+01,\n",
      "          8.2924e+00, -2.1475e+00, -5.8440e+00, -8.6253e-03, -6.0925e+00],\n",
      "        [-4.7359e+00,  8.2884e+00, -2.2384e+00, -2.3733e+00, -1.5250e+00,\n",
      "         -3.6983e+00, -3.4946e+00, -1.7314e+00, -8.4926e-01, -3.7767e+00],\n",
      "        [-5.6258e+00,  7.5618e+00, -1.3636e+00, -1.9592e+00, -1.4983e+00,\n",
      "         -2.1873e+00, -3.0584e+00, -1.8184e+00, -6.6728e-01, -3.3872e+00],\n",
      "        [-6.5738e+00,  7.3234e+00, -3.9589e+00, -2.3199e+00, -1.1560e+00,\n",
      "         -3.2814e+00, -6.0347e+00, -1.6398e-01, -4.8575e-01, -1.6567e+00],\n",
      "        [-8.9774e+00, -3.4593e+00, -8.3561e+00, -3.6814e+00,  9.8925e+00,\n",
      "         -4.4603e+00, -9.3318e+00, -6.6719e-01, -2.9683e+00,  1.5829e+00],\n",
      "        [-9.2550e+00, -8.2193e+00, -5.8005e+00, -2.1160e+00,  1.2527e-01,\n",
      "         -4.6184e+00, -1.0066e+01,  1.1118e+00, -2.2857e+00,  7.2498e+00],\n",
      "        [-8.6989e+00, -8.2684e+00, -4.8280e+00,  8.0917e+00, -9.6994e+00,\n",
      "          2.6846e+00, -1.4598e+01, -2.9627e+00, -2.5483e+00,  9.9327e-01],\n",
      "        [-3.6553e+00,  6.7365e+00, -1.5629e+00, -2.8465e+00,  5.1086e-01,\n",
      "         -6.8826e+00, -4.7499e+00,  2.3196e+00, -1.4847e+00, -3.4629e+00],\n",
      "        [-9.5678e+00, -2.2423e+00, -7.1537e+00, -1.7737e+00,  4.1430e+00,\n",
      "         -2.1935e+00, -9.1001e+00,  2.0319e+00, -2.5047e+00,  2.3343e+00],\n",
      "        [-9.3659e+00, -1.4410e+01, -2.9854e+00, -4.0298e+00, -1.0144e+01,\n",
      "          4.3713e-01, -6.1057e+00, -6.3292e+00,  6.4616e+00, -1.2749e+00],\n",
      "        [-5.1865e+00, -8.2860e+00, -9.7592e-01,  9.8107e+00, -1.7315e+01,\n",
      "         -1.5621e+00, -1.3901e+01, -5.0889e+00, -3.2792e+00, -1.3121e+00],\n",
      "        [-4.5019e+00, -2.4713e+00, -8.9892e-01, -1.7430e+00, -7.6353e+00,\n",
      "         -4.3703e+00, -6.1216e+00, -8.4042e+00,  5.6020e+00, -3.4036e+00],\n",
      "        [-6.8246e+00, -1.2829e+01, -4.8122e+00, -3.0974e+00, -8.2484e+00,\n",
      "         -8.2524e-01, -6.9191e+00, -7.3062e+00,  7.0141e+00, -3.2323e+00]],\n",
      "       grad_fn=<AddmmBackward>)\n"
     ]
    }
   ],
   "source": [
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "tianchi_metadata": {
   "competitions": [],
   "datasets": [],
   "description": "",
   "notebookId": "281737",
   "source": "dsw"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
